package com.hylink.controller.thymeleaf;

import com.hylink.base.BaseAction;
import com.hylink.bean.PageResultForBootstrap;
import com.hylink.bean.Result;
import com.hylink.bean.ResultCode;
import com.hylink.model.User;
import com.hylink.service.UserService;
import com.hylink.util.escapeWord;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController extends BaseAction{

    private Logger logger = Logger.getLogger(ThymeleafController.class);

    @Autowired
    private UserService userService;

    /**
     * 查询
     * @param name
     * @param model
     * @return
     */
    @RequestMapping("/userlist")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name,ModelMap model) {
       // model.addAttribute("name", name);
       // List<User> users = userService.getAllUsers();
       // model.p ut("users", users);

        return "thymeleaf/thymeleaf_list";
    }

    /**
     * model当使用thymeleaf标签时使用
     * @param model
     * @return
     */
    @RequestMapping(value="/userinput", method=RequestMethod.GET)
    public String input(ModelMap model ){
        logger.info("添加方法");
       return "thymeleaf/user_input";

    }

    @RequestMapping(value="/useredit", method=RequestMethod.GET)
    public String edit(ModelMap model,String id) {

        logger.info("修改方法");
        User user  = userService.getuserbyid(id);
        model.put("user",user);
        return "thymeleaf/user_edit";

    }

    @RequestMapping(value="/usersave", method=RequestMethod.POST)
    @ResponseBody
    public Result usersave(@RequestBody User user, @ModelAttribute("username") String username, ModelMap model) {
        logger.info("保存数据方法");
        try {
            if(user.getId()!=null&&!"".equals(user.getId())){
                userService.updateUser(user);

            }else {
                userService.saveUser(user);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.WARN, user);
        }
        return new Result(ResultCode.SUCCESS, user);
    }

    @RequestMapping(value="/deleteUser", method=RequestMethod.POST)
    @ResponseBody
    public Result delete(@ModelAttribute("ids") String ids, ModelMap model){
        Result result = new Result();
        if(ids!=null&&!"".equals(ids)) {
            long count = userService.delete(ids);
            result.setMsg("成功删除"+count+"条记录！");
            result.setCode(0);
        }

        return result;

     }
    /**
     * 列表demo
     * @ResponseBody 注解，它表示此接口响应为纯数据，不带任何界面展示
     * @param limit
     * @param offset
     * @param order
     * @param ordername
     * @param searchText
     * @param model
     * @return
     */
    @RequestMapping(value="/userlistjson", method= RequestMethod.GET)
    @ResponseBody
    public PageResultForBootstrap getAllUsers(Integer limit, Integer offset ,String order,String ordername, String searchText,ModelMap model) {
       HttpSession session = request.getSession();
        String    username = request.getParameter("username");
        String    userid = request.getParameter("userid");
        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put("name", escapeWord.escapeExprSpecialWord(username));
        map.put("id",escapeWord.escapeExprSpecialWord(userid));
        map.put("offset",offset);
        map.put("limit",limit);
        map.put("order",order);
        map.put("ordername",ordername);
        List<User> users = userService.getUsesWithPager(map);
        Integer total = userService.count(map);
        model.put("users", users);
        PageResultForBootstrap page = new PageResultForBootstrap();
        page.setTotal(total);
        page.setRows(users);

        return page;
    }




}
